﻿using System;
using System.Collections.Generic;
using System.Text;
using DAL;
using System.Linq;

namespace BIZ
{
    public class ServiceBIZ
    {
        public List<SERVICE> GetAll()
        {
            return WebUtils.db.SERVICEs.ToList();
        }

        public void ChangeStatus(Guid id, bool IsLock)
        {
            SERVICE s = WebUtils.db.SERVICEs.Single(service => service.ServiceID.Equals(id));
            s.IsLock = IsLock;

            WebUtils.db.SubmitChanges();
        }

        public void edit(SERVICE service)
        {
            var a = WebUtils.db.SERVICEs.Single(s => s.ServiceID.Equals(service.ServiceID));
            var category = (from c in WebUtils.db.CategoryServices where c.CategoryServiceID.Equals(service.CategoryServiceID) select c).FirstOrDefault();

            a.Title = service.Title;
            a.Url = service.Url;
            a.Content = service.Content;
            a.Image = service.Image;
            a.CategoryService = category;

            WebUtils.db.SubmitChanges();

            UrlBIZ.GetAllUrl();
        }

        public SERVICE getId(Guid id)
        {
            var service = (from s in WebUtils.db.SERVICEs where s.ServiceID.Equals(id) select s).FirstOrDefault();

            return service;
        }

        public SERVICE GetByIDForUser(Guid id)
        {
            var service = (from p in WebUtils.db.SERVICEs where p.ServiceID.Equals(id) && p.IsLock == false select p).FirstOrDefault();
            return service;
        }

        public List<SERVICE> GetByCategoryIDForUser(Guid categoryID)
        {
            var service = (from p in WebUtils.db.SERVICEs where p.CategoryServiceID.Equals(categoryID) && p.IsLock == false select p).ToList();

            return service;
        }

        public void insert(SERVICE service)
        {
            service.ServiceID = Guid.NewGuid();
            service.CreateDate = DateTime.Now;
            
            WebUtils.db.SERVICEs.InsertOnSubmit(service);
            WebUtils.db.SubmitChanges();

            UrlBIZ.GetAllUrl();
        }

        public List<SERVICE> Search(string text)
        {
            var list = (from p in WebUtils.db.SERVICEs where p.Title.Contains(text) && p.IsLock == false select p).ToList();

            return list;
        }
    }
}
